﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eis51.CampanyCMS.Model.BaseInfoMng;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.Model.BaseInfoMng.QueryO;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.LeaveMsg
{
    public partial class list : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnSearch_Click(null, null);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BaseLeaveMessageQuery query = new BaseLeaveMessageQuery();
            query.UserName = txtUserName.Text;
            query.CreateBeginDate = txtBeginDate.Text;
            query.CreateEndDate = txtEndDate.Text;
            query.IsView = rbtIsView.SelectedValue;
            query.Content = txtContent.Text;
            dgNews.Data = BaseLeaveMessageBLL.GetInstance().Seach(query);
            dgNews.DataBind();

        }

        protected void dgNews_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    BaseLeaveMessage obj = BaseLeaveMessageBLL.GetInstance().Get(new BaseLeaveMessage.Key(int.Parse(e.CommandArgument.ToString())));
                    BaseLeaveMessageBLL.GetInstance().Delete(obj);
                    this.btnSearch_Click(null, null);
                    break;
                case "View":
                    string url = "viewmessage.aspx?seqno={0}";
                    url = string.Format(url, e.CommandArgument.ToString());
                    Response.Redirect(url);
                    break;

            }
        }

        protected void dgNews_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox cbxAll = e.Item.FindControl("cbxAll") as CheckBox;
                if (cbxAll != null)
                    cbxAll.Attributes.Add("onclick", "javascript:return SelectAll('" + dgNews.ClientID + "');");
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                LinkButton btnModify = e.Item.FindControl("btnModify") as LinkButton;
                Label lblIsView = e.Item.FindControl("lblIsView") as Label;
                if (btnDelete != null)
                {
                    btnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除该留言吗?');");

                    lblIsView.Text = lblIsView.Text == "1" ? "已查看" : "未查看";
                }

            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (DataGridItem gvRow1 in dgNews.Items)
            {                
                if (((CheckBox)(gvRow1.FindControl("cbItem"))).Checked)
                {
                    string newid = gvRow1.Cells[0].Text;
                    BaseLeaveMessage obj = BaseLeaveMessageBLL.GetInstance().Get(new BaseLeaveMessage.Key(int.Parse(newid)));
                    BaseLeaveMessageBLL.GetInstance().Delete(obj);
                    
                }
            }

            btnSearch_Click(null, null);

        }
    }
}
